package com.mhys.hms.service.user.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mhys.hms.entity.Department;
import com.mhys.hms.entity.Employee;
import com.mhys.hms.mapper.EmpManagerMapper;
import com.mhys.hms.service.user.EmpManagerService;

/**
 * 部门管理业务类
 * 
 * @author
 *
 */
@Service
public class EmpManagerServiceImpl implements EmpManagerService {
	@Autowired
	private EmpManagerMapper empManagerMapper;
	@Autowired
	private DeptManagerServiceImpl deptManagerServiceImpl;

	/**
	 * 分页模糊查询
	 */
	@Override
	public List<Employee> getEmpInfo(int pageSize, int pageNumber, String deptName) {
		int startNum = (pageNumber - 1) * pageSize;
		return empManagerMapper.getEmpInfo(startNum, pageSize, deptName);
	}

	/**
	 * 根据id删除员工信息
	 */
	@Override
	public Integer delEmpInfo(String empID) {
		List<Employee> list = empManagerMapper.getEmpInfoById(empID);
		// 根据部门名称查询部门信息
		List<Department> list0 = deptManagerServiceImpl.queryDeptByName(list.get(0).getDepartName());
		// 将部门人数增1,并更新
		Department dept = list0.get(0);
		dept.setPersonNum(dept.getPersonNum() - 1);
		deptManagerServiceImpl.addDeptInfo(dept);

		return empManagerMapper.delEmpInfo(empID);
	}

	/**
	 * 查询总条数
	 */
	@Override
	public Integer getTotalNum(Integer pageNumber, Integer pageSize, String deptName) {
		int startNum = (pageNumber - 1) * pageSize;
		return empManagerMapper.getTotalNum(startNum, pageSize, deptName);
	}

	/**
	 * 新增
	 */
	@Override
	public Integer addEmpInfo(Employee emp) {
		int row = 0;
		//如果id不为null，就是更新
		if(emp.getId() != null) {
			row = empManagerMapper.updateEmpById(emp);
		}else {
			// 根据部门名称查询部门信息
			List<Department> list = deptManagerServiceImpl.queryDeptByName(emp.getDepartName());
			// 将部门人数增1,并更新
			Department dept = list.get(0);
			dept.setPersonNum(dept.getPersonNum() + 1);
			deptManagerServiceImpl.addDeptInfo(dept);
			row = empManagerMapper.addEmpInfo(emp);
		}

		return row;
	}

	/**
	 * 根据ID查询员工信息
	 */
	@Override
	public List<Employee> getEmpInfoById(String empID) {
		return empManagerMapper.getEmpInfoById(empID);
	}

}
